package report.service.dashboard.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import report.dao.po.DashboardDO;
import report.dao.po.DashboardTreeDO;
import report.dao.repository.DashboardsRepository;
import report.dao.repository.DashboardTreeRepository;
import report.dao.vo.dashboard.DashboardVO;
import report.service.dashboard.IDashboardService;
import report.service.dashboardTree.IDashboardTreeService;

@Service
public class DashboardServiceImpl implements IDashboardService {

    @Autowired
    private DashboardsRepository dashboardsRepository;
    private DashboardTreeRepository dashboardTreeRepository;
    @Autowired
    private IDashboardTreeService treeService;

    /**
     * 创建图表
     * @param pid
     * @param form
     * @return
     */
    public String createDashboard(String pid, DashboardVO form){

        // 在dashboard表中插入数据
        DashboardDO dashboardDO  = new DashboardDO();
        dashboardDO.setPid(pid);
        dashboardDO.setName(form.getName());
        dashboardDO.setConfig(form.getConfig());
        dashboardDO.setDescription(form.getDescription());
        dashboardDO.setTypeCode(form.getTypeCode());
        dashboardsRepository.save(dashboardDO);

        // 在dashboard_tree表中插入数据
        treeService.createCata(form.getPid(),form.getName());
        return "success";
    }

    /**
     * 更新图表
     * @param id
     * @param form
     * @return
     */
    public String updateDashboard(String id, DashboardVO form){

        // 在dashboard表中更新数据
        DashboardDO dashboardDO  = new DashboardDO();
        dashboardDO.setPid(form.getPid());
        dashboardDO.setId(id);
        dashboardDO.setName(form.getName());
        dashboardDO.setConfig(form.getConfig());
        dashboardDO.setDescription(form.getDescription());
        dashboardDO.setTypeCode(form.getTypeCode());
        dashboardsRepository.save(dashboardDO);

        // 在dashboard tree 表中更新数据
        DashboardTreeDO treeVO  = new DashboardTreeDO();
        treeVO.setPid(form.getPid());
        treeVO.setName(form.getName());
        treeVO.setId(id);
        treeVO.setTypeCode(form.getTypeCode());

        dashboardTreeRepository.saveAndFlush(treeVO);

        return "success";
    }

    /**
     * 删除图表
     * @param id
     * @return
     */
    public String deleteDashboard(String id){
        dashboardsRepository.delete(id);
        dashboardTreeRepository.delete(id);
        return "success";
    }
}
